//
//  TopTabBar.m
//  IntelligenceCommunity
//
//  Created by zhangpei on 15/12/28.
//  Copyright © 2015年 zhangpei. All rights reserved.
//

#import "TopTabBar.h"
#import "UtilityConfig.h"
#import "ViewHelper.h"
#import "PublicMethod.h"

@interface TopTabBar ()
{
    NSArray *buttonInfos;
    
    UIButton *currentSelectedButton;
    
    BOOL isFirstHidden;
    
}


@end

@implementation TopTabBar

@synthesize type;

@synthesize topTabBarDelegate;

@synthesize selectedBackgroundImage;

@synthesize currentSelectedIndex;

@synthesize selectedTitleColor;

@synthesize scroll;

@synthesize barButtons;

@synthesize dotImageViews;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        currentSelectedIndex = 0;
        
        barButtons = [[NSMutableArray alloc] init];
        
        dotImageViews = [[NSMutableArray alloc] init];

        [self initView];
    }
    return self;
}

- (void)initView
{
    //topTabBar
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 43)];
    scroll.backgroundColor = COLORRGBA(239, 244, 249, 1);
    
    scroll.showsHorizontalScrollIndicator = NO;
    
    scroll.showsVerticalScrollIndicator = NO;
    
    [self addSubview:scroll];
}


- (void)loadBarButtonsWithInfos:(NSArray *)infos withKey:(NSString *)key withSameWidth:(BOOL)isSame withHideFirst:(BOOL)isHide
{
    isFirstHidden = isHide;
    
    buttonInfos = infos;
    
    for (UIView *view in scroll.subviews)
    {
        [view removeFromSuperview];
    }
    
    [barButtons removeAllObjects];
    
    [dotImageViews removeAllObjects];
    
    float width = 0;
    
    if (isSame == YES)
    {
        width = scroll.bounds.size.width / infos.count;
    }
    else
    {
        if (currentSelectedIndex >= barButtons.count)
        {
            currentSelectedIndex = isHide ? 1 : 0;
        }
    }
    
    float height = scroll.bounds.size.height;
    
    CGRect lastRect = CGRectZero;
    
    for (int i = 0; i < infos.count; i++)
    {
        NSDictionary *info = [infos objectAtIndex:i];
        
        NSString *title = [info objectForKey:key];
        
        UIButton *button = [ViewHelper superView:scroll withFrame:CGRectMake(lastRect.origin.x + lastRect.size.width, 0, width, height) withNormalImage:nil withNormalBackgroundImage:nil withBackgroundColor:[UIColor clearColor] withTitle:title withFont:[UIFont boldSystemFontOfSize:14] withTag:i withTitleColor:COLORRGBA(120, 134, 123, 1) withTitleAlignment:NSTextAlignmentCenter];
        
        [button setTitleColor:selectedTitleColor forState:UIControlStateSelected];
        
        [button setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected];
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [barButtons addObject:button];
        
        if (isSame == NO)
        {
            [button sizeToFit];
            
            button.frame = CGRectMake(button.frame.origin.x, 0, button.frame.size.width + 20, height);
        }
        
        lastRect = button.frame;
        
        if (i == currentSelectedIndex)
        {
            currentSelectedButton = button;
            
            button.selected = YES;
        }
        
        UIImageView *dotImageView = [ViewHelper superView:scroll withFrame:CGRectMake(button.frame.origin.x + button.frame.size.width - 12, 9, 6, 6) withImage:nil withBackgroundColor:[UIColor redColor] withUserInteractionEnabled:YES];
        dotImageView.tag = i;
        dotImageView.hidden = YES;
        dotImageView.clipsToBounds = YES;
        dotImageView.layer.cornerRadius = 3;
        
        [dotImageViews addObject:dotImageView];
    }
    
//    [self buttonClick:currentSelectedButton];
    
    UIButton *button = [barButtons lastObject];
    
    scroll.contentSize = CGSizeMake(button.frame.origin.x + button.frame.size.width, scroll.bounds.size.height);
    
    scroll.contentOffset = CGPointMake(currentSelectedButton.frame.origin.x, 0);
}

- (void)buttonClick:(UIButton *)button
{
    currentSelectedButton.selected = NO;
    
    button.selected = YES;
    
    currentSelectedButton = button;
    
    currentSelectedIndex = button.tag;

    if (type == 1)
    {
        if (button.tag == 0)
        {
            scroll.contentOffset = CGPointMake(0, 0);
        }
        else if (button.tag < 4 && button.tag > 0)
        {
            UIButton *temp = isFirstHidden ? [barButtons objectAtIndex:1] : [barButtons objectAtIndex:0];
            
            scroll.contentOffset = CGPointMake(temp.frame.origin.x, 0);
        }
        else
        {
            scroll.contentOffset = CGPointMake(button.frame.origin.x - (scroll.bounds.size.width - button.bounds.size.width)/2, 0);
        }
        
        if (scroll.contentOffset.x + scroll.bounds.size.width > scroll.contentSize.width)
        {
            scroll.contentOffset = CGPointMake((scroll.contentSize.width - scroll.bounds.size.width), 0);
        }
    }
    
    if (topTabBarDelegate && [topTabBarDelegate respondsToSelector:@selector(topTabBarClickAtIndex:withInfo:)])
    {
        [topTabBarDelegate topTabBarClickAtIndex:button.tag withInfo:[buttonInfos objectAtIndex:button.tag]];
    }
}

- (void)selectedBarButtonAtIndex:(NSInteger)index
{
    UIButton *buton = [barButtons objectAtIndex:index];
    [self buttonClick:buton];
}


@end
